package fr.oz.mobile.module 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	import fr.digitas.flowearth.conf.Conf;
	import fr.digitas.flowearth.text.styles.styleManager;
	import fr.oz.core.datas.ProfilDatas;
	import fr.oz.core.datas.ProfilList;
	import fr.oz.core.events.ProfilEvent;
	import fr.oz.core.events.ViewEvent;
	import fr.oz.mobile.common.BGLayer_FC;
	import fr.oz.mobile.common.ui.DragDropableList;
	import fr.oz.mobile.common.ui.GradientMask;
	import fr.oz.mobile.module.profile.day.DayProfil;
	import fr.oz.mobile.module.profile.nav.ProfilNav;
	import fr.oz.mobile.module.profile.nav.ProfilNavItem;
	
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class WakeUpModule extends Sprite implements IModule
	{
		private var tfUp:TextField;
		private var tfDown:TextField;
		private var dayProfil:DayProfil;
		private var nav:ProfilNav;
		private var bg:BGLayer_FC;
		private var container_mask:GradientMask;
		private var container:DragDropableList;
		private var drag_zone:Sprite;
		private var saveProfilID:int;
		
		public function WakeUpModule() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			bg = new BGLayer_FC();
			bg.y = 125;
			addChild(bg);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			setTitle(Conf.getDatas("wakeUpProfile").data.ui.profilView);
			
			buildNav();
		}
		
		private function buildNav():void
		{
			container = new DragDropableList();
			addChild(container);
			
			nav = new ProfilNav();
			nav.addEventListener(ProfilEvent.PROFIL_SELECTED, profilView);
			//nav.y = 260;
			container.addChild(nav);
			
			container_mask = new GradientMask(22, new Rectangle(0, 260 - 35, stage.stageWidth, stage.stageHeight - 260 + 35));
			addChild(container_mask);
			
			container.cacheAsBitmap = true;
			container_mask.cacheAsBitmap = true;
			container_mask.mouseEnabled = false;
			container.mask = container_mask;
			
			container.y = 260;
			
			//container.setLimits(stage.stageHeight >> 1, stage.stageHeight >> 1, container.height);
			container.setLimits(260, stage.stageHeight - 260, container.height);
			
			drag_zone = new Sprite()
			drag_zone.graphics.beginFill(0xFF0000, 0);
			drag_zone.graphics.drawRect(0, 0, stage.stageWidth, container.height);
			drag_zone.mouseEnabled = false;
			drag_zone.graphics.endFill();
			container.addChild(drag_zone);
		}
		
		private function profilView(e:ProfilEvent):void 
		{
			if (!container.hasMoved) {
				nav.goSelect();
				
				nav.removeEventListener(ProfilEvent.PROFIL_SELECTED, profilView);
				DragDropableList.DRAGABLE = false;
				
				saveProfilID = e.profil.ID;
				dayProfil = new DayProfil(e.profil);
				dayProfil.addEventListener(ProfilEvent.PROFIL_CHANGED, handleProfileChange);
				setTimeout(createProfil, 500);
			}
		}
		
		private function handleProfileChange(e:ProfilEvent):void 
		{
			ProfilList.UPDATE(saveProfilID, dayProfil.renderProfil);
			goBack();
		}
		
		private function createProfil():void
		{
			dayProfil.addEventListener(ViewEvent.VIEW_CREATED, handleTitle);
			addChild(dayProfil);
			
			if (container.contains(nav))	container.removeChild(nav);
			
			nav = null;
			if (container.contains(drag_zone))	container.removeChild(drag_zone);
			drag_zone = null;
			if (contains(container))		removeChild(container);
			container = null;
			if (contains(container_mask))	removeChild(container_mask);
			container_mask = null;
		}
		
		private function handleTitle(e:ViewEvent):void 
		{
			setTitle(e.node);
			bg.y = e.cachePosY;
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			removeChild(tfDown);
			removeChild(tfUp);
			if (nav != null) {
				if (container.contains(nav))	container.removeChild(nav);
				nav.removeEventListener(ProfilEvent.PROFIL_SELECTED, profilView);
				nav = null;
				if (container.contains(drag_zone))	container.removeChild(drag_zone);
				drag_zone = null;
				if (contains(container))		removeChild(container);
				container = null;
				if (contains(container_mask))	removeChild(container_mask);
				container_mask = null;
			}
			if (dayProfil != null)
			{
				if (contains(dayProfil))	removeChild(dayProfil);
				dayProfil.removeEventListener(ViewEvent.VIEW_CREATED, handleTitle);
				dayProfil = null;
			}
		}
		
		private function setTitle(node:XMLList):void
		{
			if (tfDown != null)
			{
				removeChild(tfDown);
				removeChild(tfUp);
			}
			tfDown = new TextField();
			tfUp = new TextField();
			styleManager.apply(tfUp, 
								node.name.up.@cssId, 
								node.name.up);
			styleManager.apply(tfDown, 
								node.name.down.@cssId, 
								node.name.down);
			addChild(tfDown);
			addChild(tfUp);
			
			tfUp.y = 35;
			tfDown.y = tfUp.y + tfUp.height - 10;
			tfUp.x = ((stage.stageWidth - tfUp.width) >> 1) >> 0;
			tfDown.x = ((stage.stageWidth - tfDown.width) >> 1) >> 0;
		}
		
		/* INTERFACE fr.oz.mobile.module.IModule */
		
		public function resize():void
		{
			
		}
		
		public function activeView():void
		{
			
		}
		
		public function removeView():void
		{
			
		}
		
		public function goBack():Boolean
		{
			if (dayProfil == null) {
				return false;
			}else {
				setTitle(Conf.getDatas("wakeUpProfile").data.ui.profilView);
				
				buildNav();
				
				bg.y = 125;
				
				dayProfil.removeEventListener(ViewEvent.VIEW_CREATED, handleTitle);
				removeChild(dayProfil);
				dayProfil = null;
			}
			return true;
		}
		
	}

}